/**
 * Created by Administrator on 2017/3/28.
 */
/*对表单进行校验*/
function oncheck() {
    var username = document.getElementById("username").value;
    if (username == null || username == "") {
        alert("用户名不能为空");
        return false;
    }
    var password = document.getElementById("password").value;
    if (password == null || password == "") {
        alert("密码不能为空");
        return false;
    }
    var repassword = document.getElementById("repassword").value;
    if (repassword == null || repassword == "") {
        alert("确认密码不能为空");
        return false;
    }
    if (repassword != password) {
        alert("两次密码输入不一致");
        return false;
    }

    var email = document.getElementById("email").value;
    if (email == null || email == "") {
        alert("邮箱不能为空");
        return false;
    }
    var phone = document.getElementById("phone").value;
    if (phone == null || phone == "") {
        alert("电话不能为空");
        return false;
    }
    var validatecode = document.getElementById("validatecode").value;
    if (validatecode == null || validatecode == "") {
        alert("验证码不能为空");
        return false;
    }
}

// ajax校验用户名是否存在

function checkByUsername() {
    //获取用户名
    var username = document.getElementById("username").value;
    /*1.创建xmlHttpRequest对象*/
    var xmlHttp = createXmlHttpRequest();
    /*2.连接服务器*/
    xmlHttp.open("POST", "${pageContext.request.contextPath}/user_findByUsername.action", true);
    //设置请求头
    xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    /*3.发送请求(指定的请求体)*/
    xmlHttp.send("username=" + username + "&date=" + new Date().getTime());
    /*4.响应*///给异步对象的onreadystatechange事件注册监听器
    xmlHttp.onreadystatechange = function () { //当xmlHttp的状态变化为4且响应码为200时
        if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
            var flag = xmlHttp.responseText;
            var msg = document.getElementById("u");
            if(flag == "true"){
                msg.innerHTML="可以注册";
               msg.style.color="green";
             /*alert("用户名可以使用！");*/
            }else {
                msg.innerHTML="用户名已经被占用";
                msg.style.color="red";
            }
        }
    }

}
function  createXmlHttpRequest() {
    try {
        return new XMLHttpRequest();
    } catch (e) {
        try {
            return new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                return new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {
                alert("浏览器有问题呀。。。。")
                throw e;
            }
        }
    }
};

/*//切换验证码
function changecode(){
    var img = document.getElementById("validateimage");
    img.src = "${pageContext.request.contextPath}/codeaction?a="+new Date().getTime();
}*/
